var createEditForm = function(selNode)
{	
	Ext.Ajax.request({
		url    : 'distributor/getElement',
		params : {
			node: selNode.id
		},
		success: function(response, opts) { 
			var responseJson = Ext.decode(response.responseText);
			if (responseJson.success == "true") {
				
				var selNodePath = selNode.getPath();
				var treePanel = Ext.getCmp('companyTreePanel');
				
				var IsCommonNotPerson;
				
				if ((selNode.id =='CompanyTreeRoot') || 
					(selNode.id.match('Division')) || 
					(selNode.id.match('Project'))) {
					
					IsCommonNotPerson = true;
	    		}
				else {
					IsCommonNotPerson = false;
				}
				
				if (IsCommonNotPerson) {
					var ptitle = responseJson.title;
					var pcemail = responseJson.email;
					var pstartDate = responseJson.startDate;	
				} else {
					var pname = responseJson.name;
					var ppemail = responseJson.email;
					var pbirthDate = responseJson.birthDate;
					var phiredDate = responseJson.hiredDate;					
				}
											
				var refTo;		
				var refToLabel;		
				if (selNode.id.match('Project')) {
					refTo = responseJson.divisionTo;
					refToLabel = 'divisionTo';
				}
				else {
					refTo = responseJson.projectTo;
					refToLabel = 'projectTo';
				}
				
				if (selNode.id =='CompanyTreeRoot') {
					var wTitle = "Info: Company";
	    		}
				else if (selNode.id.match('Division')) {
					var wTitle = "Edit Division";
				}
				else if (selNode.id.match('Project')) {
					var wTitle = "Edit Project";
				}
				else {
					var wTitle = "Edit Person";
					var afieldLabel = 'in project';
					var aUrl = 'project/getlistAll';
				}
				
				if (IsCommonNotPerson) {
					var afieldLabel = 'in division';
					var aUrl = 'division/getlistAll';
					
					var formCommonTextAreaTitle = new Ext.form.TextField({
						xtype: 'textfield',
						id: 'title',
						fieldLabel: 'title',
						value: ptitle,
						allowBlank:false,
						anchor: '100%'		
					});
					
					var formCommonTextAreaEmail = new Ext.form.TextField({
						xtype: 'textfield',
						id: 'email',
						fieldLabel: 'email',
						value: pcemail,
						allowBlank:true,
						anchor: '100%'		
					});
					
					var formCommonStartDateField = new Ext.form.DateField({
						xtype: 'Datefield',
						id: 'startDate',
						fieldLabel: 'StartDate',
						value: new Date(pstartDate),
						allowBlank:true,
						anchor: '100%'		
					});
					
					var formCommonReferTo = new Ext.form.ComboBox({
						xtype: 'combo',
						id: 'formCommonReferTocombo',
					    fieldLabel: afieldLabel,
					    hiddenName: refToLabel,
					    displayField: 'text',
					    typeAhead: true,
					    triggerAction: 'all',
					    emptyText:'Choose division...',
					    selectOnFocus:true,
					    store: {
					        xtype : 'jsonstore',
					        url: aUrl,
					        fields: ['text','id'],
					        autoLoad: true,
							listeners: {
								load: function() {
									var combo = Ext.getCmp("formCommonReferTocombo");
									combo.setValue(refTo);
								},
								scope:this
							}
					    }
					});
					
					var aItems;
					
					if (selNode.id =='CompanyTreeRoot') {
						aItems = [formCommonTextAreaTitle, formCommonTextAreaEmail, formCommonStartDateField];
		    		}
					else if (selNode.id.match('Division')) {
						aItems = [formCommonTextAreaTitle, formCommonTextAreaEmail, formCommonStartDateField];
					}
					else if (selNode.id.match('Project')) {
						aItems = [formCommonTextAreaTitle, formCommonTextAreaEmail, formCommonStartDateField, formCommonReferTo];
					}
					
				} else {
					var formPersonTextAreaName = new Ext.form.TextField({
						xtype: 'textfield',
						id: 'name',
						fieldLabel: 'name',
						value: pname,
						allowBlank:false,
						anchor: '100%'		
					});
					
					var formPersonTextAreaEmail = new Ext.form.TextField({
						xtype: 'textfield',
						id: 'email',
						fieldLabel: 'email',
						value: ppemail,
						allowBlank:true,
						anchor: '100%'		
					});
					
					var formPersonBirthDateField = new Ext.form.DateField({
						xtype: 'Datefield',
						id: 'birthDate',
						fieldLabel: 'BirthDate',
						value: new Date(pbirthDate),
						allowBlank:true,
						anchor: '100%'		
					});
					
					var formPersonHiredDateField = new Ext.form.DateField({
						xtype: 'Datefield',
						id: 'hiredDate',
						fieldLabel: 'HiredDate',
						value: new Date(phiredDate),
						allowBlank:true,
						anchor: '100%'		
					});
					
					var formPersonReferTo = new Ext.form.ComboBox({
						xtype: 'combo',
						id: 'formPersonReferTocombo',
					    fieldLabel: afieldLabel,
					    hiddenName: refToLabel,
					    displayField: 'text',
					    typeAhead: true,
					    triggerAction: 'all',
					    emptyText:'Choose project...',
					    selectOnFocus:true,
					    store: {
					        xtype : 'jsonstore',
					        url: aUrl,
					        fields: ['text','id'],
					        autoLoad: true,
							listeners: {
								load: function() {
									var combo = Ext.getCmp("formPersonReferTocombo");
									combo.setValue(refTo);
								},
								scope:this
							}
					    }
					});
					
					var aItems =  [formPersonTextAreaName, formPersonTextAreaEmail, formPersonBirthDateField, formPersonHiredDateField, formPersonReferTo];
				}
				
				var form = new Ext.form.FormPanel({
			        baseCls: 'x-plain',
			        labelWidth: 65,
			        defaultType: 'textfield',
			        items: aItems
			    });
				
			    var window = new Ext.Window({
			    	title  : wTitle,
			    	width  : 400,
			    	layout : 'fit',
			        plain:true,
			        bodyStyle:'padding:5px;',
			        buttonAlign:'center',
			        items: form,

			        buttons: [{
			        	text: 'Save',
			        	handler: function() {
			        		form.getForm().submit({
			        			url : 'distributor/editElement',
			        			params : {
			        				node : selNode.id
			        			},
			        			waitTitle: 'please wait...',
			        			waitMsg: 'wait...',
			        			success: function(form, action) {
			        				window.close();	                	
			        				root = Ext.getCmp('companyTreePanel').getRootNode();
			        				root.reload();
			        				treePanel.expandPath(selNodePath);
			        			},
			        			failure: function(form, action) {
			        				Ext.Msg.alert('An error occured with the server.');
			        			}
			        		});
			        	}
			        }]
			    });

			    window.show();
			}
			else {
				Ext.Msg.alert('An error occured with the server.');
			}
		}
	});
};